package com.twetec.cms.adapter.pc.backend.aop;

import com.twetec.cms.adapter.pc.backend.annotion.SitePermission;
import com.twetec.cms.adapter.pc.backend.exception.PermissionException;
import com.twetec.cms.common.exception.PermissionExceptionEnum;
import com.twetec.cms.common.util.cache.RedisCache;
import com.twetec.cms.common.util.jwt.JWTUtil;
import com.twetec.cms.service.ucenter.UserService;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @Author: zhaichangzheng
 * @Date: 2022/2/10 17:28 （日期和时间）
 */
@Aspect
@Component
public class SitePermissionAop {

//    @Autowired
//    private RedisCache redisCache;

    @Autowired
    private UserService userService;

    private static Logger logger = LoggerFactory.getLogger(SitePermissionAop.class);

    /**
     * 权限切入点
     */
    @Pointcut("@annotation(com.twetec.cms.adapter.pc.backend.annotion.SitePermission)")
    private void gerPermissionPointCut() {

    }

    /**
     * 前置增强
     * @param joinPoint
     */
    @Before("gerPermissionPointCut()")
    public void doPermission(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        SitePermission annotation = method.getAnnotation(SitePermission.class);

        // 当前方法需要的value值
        String[] value = annotation.value();

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String siteId = request.getParameter("siteId");
        String columnId = request.getParameter("columnId");

        if (null == columnId) {
            columnId = "0";
        }

        // 权限标识是siteId：columnId：requireRoles
        String permission = siteId + ":" + columnId + ":" + value[0];

        // 获取用户id，并从redis中获取用户的权限集合
        String authorization = request.getHeader("Authorization");
        // 如果authorization存在，则解析出jwt
        String jwt = StringUtils.substringAfter(authorization, "Bearer ");

        // 获取用户的菜单权限
        Long userId = JWTUtil.parseUserId(jwt);
        List<Object> permissionMenuListFromRedis = userService.getPermissionMenuListFromRedis(userId);

//         如果没有此权限
        if (!permissionMenuListFromRedis.contains(permission)) {
            throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION);
        }

    }

}
